using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using TTDotNet.Engine.ThreeDEngine;

namespace TTDotNet.Engine.GUI
{
    public class Label : Control
    {
        
        public Label(RectangleF screenRect, string text, float fontSize, Color textColor, string fontName, GameTexture up, DrawTextFormat format)
        {
            _size = screenRect;
            _size.Width = up.Width;
            _size.Height = up.Height;
            _text = text;
            _fontSize = fontSize;
            _fontName = fontName;
            _fontColorDeselected = textColor;
            _fontColorSelected = textColor;
            _fontColor = textColor;
            _position = new PointF(screenRect.X, screenRect.Y);
            textureUp = up;

            _textRect = screenRect;
            textFormat = format;

            systemFont = new System.Drawing.Font(fontName, fontSize);
            
            _font = new Microsoft.DirectX.Direct3D.Font(Game.device, systemFont);

            if(!_text.Equals(String.Empty))
                _font.PreloadText(_text);

            SetSize(screenRect.Width, screenRect.Height);
        }

        public override void Render(PointF location)
        {
            Game.guiSprite.Transform = spriteTransform;
            Game.guiSprite.Draw(textureUp.Texture, Rectangle.Empty, Vector3.Empty, Vector3.Add(_renderPosition, new Vector3(location.X / WidthScale, location.Y / HeightScale, 0)), Color.White);

            if (!_text.Equals(string.Empty))
            {
                Game.guiSprite.Transform = Matrix.Identity;
                Game.arial14.DrawText(Game.guiSprite, _text, new Rectangle((int)(_textRect.X + location.X), (int)(_textRect.Y + location.Y), (int)_textRect.Width, (int)_textRect.Height), textFormat, _fontColor);
                //Game.arial14.DrawText(Game.guiSprite, _text, Rectangle.Empty, textFormat, _fontColor);
                Game.guiSprite.Transform = spriteTransform;
            }

            //if (!MultiLine)
            //{
                //if (Game.toggle1)
                //{
                    
                //}
            //}
            //else
            //{
            //    RectangleF textSize = _font.MeasureString(null, _text, textFormat, _fontColor);

            //    //FIX - Slow with splitting, measuring and managed objects.
            //    if (textSize.Width > _size.Width)
            //    {
            //        float spaceWidth = _font.MeasureString(null, "a b", textFormat, _fontColor).Width - _font.MeasureString(null, "ab", textFormat, _fontColor).Width;

            //        List<string> splitStrings = new List<string>(_text.Split(new char[]{' '}));
            //        List<string> splitUp = new List<string>();

            //        float runningX = 0;
            //        string currentWord = "";

            //        foreach (string s in splitStrings)
            //        {
                        
            //            float newSLength = _font.MeasureString(null, s, textFormat, _fontColor).Width + spaceWidth;

            //            if (runningX + newSLength < _size.Width)
            //            {
            //                runningX += newSLength;
            //                currentWord += s + " ";
            //            }
            //            else
            //            {
            //                splitUp.Add(currentWord);
            //                currentWord = s + " ";
            //                runningX = 0;

            //            }
            //        }
            //        splitUp.Add(currentWord);

            //        int lineNum = 0;

            //        foreach (string line in splitUp)
            //        {
            //            _font.DrawText(null, line, new Rectangle((int)(_textRect.X + location.X), (int)(_textRect.Y + location.Y + (lineNum++ * (int)_textRect.Height)), (int)_textRect.Width, (int)_textRect.Height),
            //                    textFormat, _fontColor);
            //        }

            //    }
            //    else
            //    {
            //        _font.DrawText(null, _text, new Rectangle((int)(_textRect.X + location.X), (int)(_textRect.Y + location.Y), (int)_textRect.Width, (int)_textRect.Height),
            //                    textFormat, _fontColor);
            //    }

                
            //}
        }

    }
}
